package com.cyber.app_container.exception;

import lombok.Getter;

/**
 * 全局错误码枚举（按模块分层，便于维护）
 * 错误码格式：模块_HTTP状态码_序号（示例：USER_404_001 → 用户模块，404状态，第1个错误）
 */
@Getter
public enum ErrorCode {
    // 全局通用模块
    SUCCESS("G000000", "成功"),
    SYSTEM_ERROR("G500001", "系统繁忙，请稍后再试"),
    INVALID_PARAM("G400001", "请求参数不合法"),
    REPEATED_REQUEST("G400002", "重复请求，请稍后重试"),
    DATA_NOT_FOUND("G404001", "请求数据不存在"),

    // 用户模块
    USER_DISABLED("U403001", "用户已被禁用"),
    USER_NOT_FOUND("U404001", "用户不存在"),
    USERNAME_DUPLICATE("U409001", "用户名已存在"),
    ACCOUNT_EMPTY("U400001", "账号不能为空"),
    PASSWORD_EMPTY("U400002", "密码不能为空"),
    USERNAME_LENGTH_INVALID("U400003", "用户名长度应在3-20字符"),
    EMAIL_EMPTY("U400004", "邮箱不能为空"),
    EMAIL_FORMAT_INVALID("U400005", "邮箱格式不正确"),
    PASSWORD_LENGTH_INVALID("U400006", "密码长度应在8-32字符"),

    // 认证模块
    UNAUTHORIZED("A401001", "认证失败，请重新登录"),
    CREDENTIALS_MISMATCH("A401002", "账号或密码错误"),
    TOKEN_INVALID("A401003", "令牌无效或已过期"),
    ACCESS_DENIED("A403001", "无权限访问该资源"),

    // 文件模块
    FILE_UPLOAD_FAIL("F500001", "文件上传失败"),
    FILE_TYPE_INVALID("F400001", "不支持的文件类型");

    private final String code;

    private final String message;

    ErrorCode(String code, String message) {
        this.code = code;
        this.message = message;
    }

    public static ErrorCode fromCode(String code) {
        for (ErrorCode errorCode : ErrorCode.values()) {
            if (errorCode.getCode().equals(code)) {
                return errorCode;
            }
        }
        throw new IllegalArgumentException("未知错误码: " + code);
    }
}
